% Dataset sort by importance/size
DatasetList = { ...
    'Ddavid_CAL500_normal', ...
    'Ddavid_yeast', ...
    };
% DatasetList = { ...
%     'Ddavid_CAL500_normal', ...
%     'Ddavid_yeast', ...
%     'Ddavid_emotions_normal', ...
%     'Ddavid_scene', ...
%     'Ddavid_mediamill_small', ...
%     'Ddavid_birds' ...
%     'Ddavid_NUS-WIDE-cVLADplus' ...
%     };
DatasetListSize = size(DatasetList, 2);

DatasetDir = 'D:\School\Meeting\Program\Matlab\Dataset\Multi Label\';
StartDir = 'D:\School\Meeting\Program\Matlab\Result\kNN_Recover_Ver2\';
ExperimentTimes = 1;
UsedKList = [3, 11, 19, 27];
PercentLabeledDataList = [20, 80];
PercentKeepingLabelsList = [25];
FoldNumberList = [1];

ValidRate = 0.2;

% Dataset name
for NameC = 1:DatasetListSize
    Dataset = DatasetList{NameC};
    
    disp(Dataset);
    
    % Different K for the recovering
    for l = UsedKList
        UsedK = l;
        
        % Percent of labeled data
        for i = PercentLabeledDataList
            PercentLabeledData = i;
            
            % Percent of keeping labels
            for j = PercentKeepingLabelsList
                PercentKeepingLabels = j;
                
                for k = FoldNumberList
                    FoldNumber = k;
                    
                    DatasetFolderName = [DatasetDir Dataset];
                    cd(DatasetFolderName);
                    DatasetFilename = [Dataset '_' int2str(PercentLabeledData) '_' int2str(PercentKeepingLabels) '_Fold' int2str(FoldNumber)];
                    
                    % Make Full KNN List Code
                    load(DatasetFilename);
                    
                    N = size(SampledTrueLabelTraining, 1);
                    M = size(SampledTrueLabelTraining, 2);
                    
                    FullKNNListTrainingToTraining = Ddavid_find_knn(size(AllDataTraining, 1) - 1, AllDataTraining);
                    FullKNNListTestingToTraining = Ddavid_find_knn_from_training_data(size(AllDataTraining, 1), AllDataTesting, AllDataTraining);
                    FullKNNListSampTrainingToSampTraining = Ddavid_find_knn(size(SampledOnlyDataTraining, 1) - 1, SampledOnlyDataTraining);
                    FullKNNListTestingToSampTraining = Ddavid_find_knn_from_training_data(size(SampledOnlyDataTraining, 1), AllDataTesting, SampledOnlyDataTraining);
                    
                    % Calculate the sampling rate constant C
                    MLkNNCTable = zeros(1, M);
                    for LabelCounter = 1:M
                        MLkNNCTable(1, LabelCounter) = Ddavid_get_sampling_rate_C_by_MLkNN(AllDataTraining, SampledTrueLabelTraining(:, LabelCounter), ValidRate);
                    end

                    save(DatasetFilename, 'AllData', 'TrueLabel', 'AllDataSampler', 'AllDataTraining', 'TrueLabelTraining', 'AllDataTesting', 'TrueLabelTesting', 'SampledTrueLabelTraining', 'SampledOnlyDataTraining', 'SampledOnlyTrueLabelTraining', 'UnsampledOnlyDataTraining', 'DataSampler', 'SampledDataSize', 'SampledLabelSize', 'Incomplete_Label_Preprocessing_Option', 'FullKNNListTrainingToTraining', 'FullKNNListTestingToTraining', 'FullKNNListSampTrainingToSampTraining', 'FullKNNListTestingToSampTraining', 'MLkNNCTable');

                    %%% Recover times
                    % for CurrentExperimentTimes = 1:ExperimentTimes
                    %     Times = CurrentExperimentTimes;
                    %     
                    %     ResultFolderName = [Dataset '_K' int2str(UsedK)];
                    %     ResultFilename = [Dataset '_' int2str(PercentLabeledData) '_' int2str(PercentKeepingLabels) '_Fold' int2str(FoldNumber) '_kNNRecover_Results_T' int2str(Times)];
                    % 
                    %     Ddavid_recovered_result_analyze(DatasetFilename, ResultFolderName, ResultFilename);
                    % end
                end
            end
        end
    end
end

sound(sin(2 * pi * 25 * (1:1000) / 400));
